package com.tencent.miniproject.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tencent.miniproject.R;
import com.tencent.miniproject.base.BaseRecyclerViewAdapter;
import com.tencent.miniproject.been.NeighborRecommand;
import com.tencent.miniproject.been.Post;
import com.tencent.miniproject.constants.Constant;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class UserBillAdapter extends BaseAdapter {

    private Context mContext;
    private UserBillInterface mListener;    //接口实例
    private List<Post> mDataList;

    public UserBillAdapter(Context context) {
        mContext = context;
        mDataList = new ArrayList<>();
    }

    public void setUserBillListener(UserBillInterface listener){
        mListener = listener;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Post getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemUserBillHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_user_bill,
                    parent, false);
            holder = new ItemUserBillHolder();
            holder.tvStoreName = convertView.findViewById(R.id.tv_store_name);
            holder.tvPrice = convertView.findViewById(R.id.tv_price);
            holder.tvPosition = convertView.findViewById(R.id.tv_position);
            holder.tvContent = convertView.findViewById(R.id.tv_content);
            holder.ivPic = convertView.findViewById(R.id.iv_pic);
            holder.tvShowVotes = convertView.findViewById(R.id.tv_show_votes);
            holder.tvShowComments = convertView.findViewById(R.id.tv_show_comments);
            holder.llShowVotes = convertView.findViewById(R.id.ll_votes);
            convertView.setTag(holder);
            //设置监听
            setOnclickListener(holder, position);
        }else {
            holder = (ItemUserBillHolder) convertView.getTag();
        }
        Post post = getItem(position);
        Glide.with(mContext)
                .load(post.getImgUrl())
                .into(holder.ivPic);
        holder.tvStoreName.setText(post.getStore());
        holder.tvPrice.setText("￥" + post.getCost());
        holder.tvPosition.setText(post.getPosition());
        holder.tvContent.setText(post.getContent());
        holder.tvShowVotes.setText(String.valueOf(post.getVotes()));
        holder.tvShowComments.setText(String.valueOf(post.getComments()));
        return convertView;
    }

    /**
     * 设置点击事件
     * @param holder holder
     */
    private void setOnclickListener(ItemUserBillHolder holder, final int position){
        holder.tvPosition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("ning", "onItemClick22: " + position);
                mListener.putPostIdToMapActivity(getItem(position));
            }
        });
        holder.tvStoreName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.putPostIdToMapActivity(getItem(position));
            }
        });
        holder.tvShowComments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //进入详情页
                mListener.jumpToDetailActivity(getItem(position));
            }
        });
        holder.llShowVotes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.jumpToDetailActivity(getItem(position));
            }
        });
    }

    public void updateData(List<Post> userBillList) {
        mDataList.clear();
        mDataList.addAll(userBillList);
        notifyDataSetChanged();
    }

    public interface UserBillInterface{
        //点击位置，跳转到前端地图页面
        void putPostIdToMapActivity(Post post);
        void jumpToDetailActivity(Post post);
    }

    class ItemUserBillHolder {
        TextView tvStoreName;
        TextView tvPrice;
        TextView tvPosition;
        TextView tvContent;
        ImageView ivPic;
        TextView tvShowVotes;
        TextView tvShowComments;
        LinearLayout llShowVotes;   //点击跳转详情页
    }

}
